package com.lang8.util.ioc;

import java.util.HashMap;
import java.util.Map;


@SuppressWarnings("rawtypes")
public class BeanManager {
	private static BeanManager instance = new BeanManager();
	private static boolean isInit = false;
	private Map<Class, Object> beanMap = new HashMap<Class, Object>();
	private BeanManager(){}
	public static BeanManager getInstance(){
		if (!isInit) {
			synchronized (BeanManager.class) {
				if (!isInit) {
					instance.init();
					isInit = true;
				}
			}
		}
		return instance;
	}
	private void init() {
	}
	private Object generateBean(Class clazz) throws InstantiationException, IllegalAccessException {
		Object obj = clazz.newInstance();
		return obj;
	}
	public static Object getBean(Class clazz) {
		BeanManager bm = getInstance();
		Object bean = bm.beanMap.get(clazz);
		if (bean == null) {
			try {
				bean = bm.generateBean(clazz);
				bm.beanMap.put(clazz, bean);
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return bean;
	}
	

}
